package com.fhz.room;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.os.Bundle;
import android.view.View;

import com.fhz.room.databinding.ActivityMainBinding;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    AppDatabase db;
    ActivityMainBinding mBinding;
    //线程池的使用
    ExecutorService mExecutorService = Executors.newFixedThreadPool(5);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());


        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "a2102")
                .allowMainThreadQueries()
                .build();
        UserDao userDao = db.userDao();
        StudentDao studentDao = db.studentDao();
        TeacherDao teacherDao = db.teacherDao();


        mBinding.selectAll.setOnClickListener(v -> {

            Runnable runnable = new Runnable(){
                @Override
                public void run() {
                    List<User> all = userDao.getAll();
                    for (User user : all) {
                        System.out.println(user);
                    }

                }
            };
            mExecutorService.execute(runnable);
        });

        User user = new User();
        user.firstName = "feng";
        user.lastName = "haha";
        mBinding.insert.setOnClickListener(v ->{
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    userDao.insertAll(user);
                }
            };

            mExecutorService.execute(runnable);
        });

        mBinding.update.setOnClickListener(v -> {
            user.uid = 1;
            user.firstName = "feng1";
            user.lastName = "haha1";
            userDao.updateUser(user);
        });

        mBinding.delte.setOnClickListener(v  -> {
            user.uid = 3;
            user.firstName = "feng";
            user.lastName = "haha";
            userDao.delete(user);
        });

        mBinding.selectUserById.setOnClickListener(v -> {
            User userById = userDao.findUserById(1);
            System.out.println(userById);
        });

        mBinding.selectUserByNameAndPwd.setOnClickListener(v  -> {
            User userByNameAndPwd = userDao.findUserByNameAndPwd("feng1", "haha1");
            System.out.println(userByNameAndPwd);
        });


        mBinding.selectUserLikeName.setOnClickListener(v -> {
            //模糊查询的写法,有点不得劲
            List<User> fe = userDao.findByName("fe%");
            for (User user1 : fe) {
                System.out.println(user1);
            }
        });


        mBinding.insertStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = new Student();
                student.firstName="大明";
                studentDao.insertAll(student);

                Teacher teacher = new Teacher();
                teacher.firstName="老明";
                teacherDao.insertAll(teacher);
            }
        });

        mBinding.selectAllStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                List<Student> all = studentDao.getAll();
//                for (Student student : all) {
//                    System.out.println(student);
//                }

                List<Teacher> all1 = teacherDao.getAll();
                for (Teacher teacher : all1) {
                    System.out.println(teacher);
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mExecutorService.shutdown();
    }
}